package com.abhimanyu.service;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.abhimanyu.service.model.DollarRateServiceData;

public class CompareRemitDollarRateObserver extends AbstractDollarRateObserver {
	private static final String PARSE_START = "<aside class=\"carousel-main\">";
	private static final String PARSE_END = "</aside>";

	public CompareRemitDollarRateObserver() {
		super.setUrl("http://www.compareremit.com/");
	}

	public List<DollarRateServiceData> getCurrentDollarRate() {
		List<DollarRateServiceData> serviceModelList = null;
		try {
			String webPageContent = executeDollarService();

			String dataContent = getDollarRate(webPageContent);

			serviceModelList = parseCompareRemitData(dataContent);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return serviceModelList;
	}

	public List<DollarRateServiceData> parseCompareRemitData(String dataContent) {
		List<DollarRateServiceData> serviceDataList = new ArrayList<DollarRateServiceData>();
		DollarRateServiceData serviceData = null;
		try {

			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			InputSource is = new InputSource(new StringReader(dataContent));
			Document doc = dBuilder.parse(is);

			doc.getDocumentElement().normalize();

			NodeList aList = doc.getDocumentElement().getElementsByTagName("a");

			for (int i = 0; i < aList.getLength(); i++) {
				String serviceName = null;
				String dollarValue = null;
				serviceData = new DollarRateServiceData();
				Node imgItem = null;
				for (int j = 0; j < aList.item(i).getChildNodes().item(0)
						.getAttributes().getLength(); j++) {
					if (aList.item(i).getChildNodes().item(0).getAttributes()
							.item(j).getNodeType() == Node.ATTRIBUTE_NODE) {
						imgItem = aList.item(i).getChildNodes().item(0)
								.getAttributes().item(j);
						break;
					}
				}

				serviceName = imgItem.getNodeValue().trim();

				Node node = (Node) aList.item(i).getNextSibling()
						.getNextSibling();

				NodeList spanList = node.getChildNodes();
				for (int l = 0; l < spanList.getLength(); l++) {

					if (!spanList.item(l).getTextContent().trim().isEmpty()) {
						try {
							dollarValue = spanList.item(l).getTextContent()
									.trim();
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				}
				serviceData.setDollarRateData(dollarValue);
				serviceData.setServiceName(serviceName);
				serviceDataList.add(serviceData);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return serviceDataList;

	}

	private String getDollarRate(String webPageContent) {
		/*
		 * System.out.println(webPageContent.indexOf(PARSE_START));
		 * System.out.println(webPageContent.indexOf(PARSE_END) +
		 * webPageContent.indexOf(PARSE_START));
		 */
		String asideContent = webPageContent.substring(
				webPageContent.indexOf(PARSE_START),
				webPageContent.indexOf(PARSE_START)
						+ webPageContent.indexOf(PARSE_END));

		asideContent = asideContent.substring(asideContent.indexOf(PARSE_START)
				+ PARSE_START.length(), asideContent.indexOf(PARSE_END));
		asideContent = asideContent.replaceAll("></a>", "/></a>");
		return asideContent.trim();

	}

	public static void main(String[] args) {
		System.out.println(new CompareRemitDollarRateObserver()
				.getCurrentDollarRate());
	}

	@Override
	public String toString() {
		return "CompareRemit";
	}
}
